<template>
	<view class="columnbox wdh-100 content">
		<view class="info-box columnbox wdh-100">
			<view class="columnbox one-info jus als">
				<view class="info-title wdh-100">病情描述</view>
				<view class="columnbox als wdh-100" style="margin-top: 20rpx;">
					<textarea placeholder-class="reg-pl" class="wdh-100 info-input" v-model="describe"
						placeholder="请描述您的疾病或症状"></textarea>

				</view>
			</view>
		</view>
		<view class="info-box columnbox wdh-100">
			<view class="columnbox one-info jus als">
				<view class="info-title">检查报告或患处照片(选填)</view>
				<view class="img-list wdh-100">
					<view v-for="(item,index) in imgList" :key='index' class="rowbox one-img-box"
						@click="deletImg(index)">
						<image class="one-img" mode="aspectFill" :src="item.url"></image>
						<view class="close-icon">
							<u-icon name="close-circle-fill" size="32rpx"></u-icon>
						</view>
					</view>
					<image @click="addImg" class="one-img" src="/static/img/addimg.png" mode="aspectFill">
					</image>
				</view>
			</view>
		</view>
		<view class="columnbox one-info jus">
			<view class="info-title wdh-100">疾病史</view>
			<view class="rowbox wdh-100 jus" style="margin-top: 32rpx;">
				<view class="info-title">过敏史：</view>
				<radio-group v-model="guominshi" @change="guominshi=$event.detail.value">
					<radio color="#00b6b2" style="transform: scale(.7);margin-right: 40rpx;" :checked="guominshi=='1'"
						value="1">
						<view style="transform: scale(1.5);margin-left: 20rpx;">无</view>
					</radio>
					<radio color="#00b6b2" style="transform: scale(.7);" :checked="guominshi=='2'" value="2">
						<view style="transform: scale(1.5);margin-left: 20rpx;">有</view>
					</radio>
				</radio-group>
			</view>

			<view class="rowbox wdh-100 jus" style="margin-top: 32rpx;">
				<view class="info-title">肝/肾功能：</view>
				<radio-group v-model="ganshen" @change="ganshen=$event.detail.value">
					<radio color="#00b6b2" style="transform: scale(.7);margin-right: 40rpx;" :checked="ganshen=='1'"
						value="1">
						<view style="transform: scale(1.5);margin-left: 20rpx;">正常</view>
					</radio>
					<radio color="#00b6b2" style="transform: scale(.7);" :checked="ganshen=='2'" value="2">
						<view style="transform: scale(1.5);margin-left: 20rpx;">异常</view>
					</radio>
				</radio-group>
			</view>

			<view class="rowbox wdh-100 jus" style="margin-top: 32rpx;">
				<view class="info-title">状态：</view>
				<radio-group v-model="puru" @change="puru=$event.detail.value">
					<radio color="#00b6b2" style="transform: scale(.7);" :checked="puru=='1'" value="1">
						<view style="transform: scale(1.5);margin-left: 20rpx;">无</view>
					</radio>
					<radio color="#00b6b2" style="transform: scale(.7);" :checked="puru=='2'" value="2">
						<view style="transform: scale(1.5);margin-left: 20rpx;">备孕</view>
					</radio>
					<radio color="#00b6b2" style="transform: scale(.7);" :checked="puru=='3'" value="2">
						<view style="transform: scale(1.5);margin-left: 20rpx;">妊娠</view>
					</radio>
					<radio color="#00b6b2" style="transform: scale(.7);" :checked="puru=='4'" value="2">
						<view style="transform: scale(1.5);margin-left: 20rpx;">哺乳</view>
					</radio>
				</radio-group>
			</view>
		</view>

		<view style="width: 100%;height: 200rpx;"></view>
		<view class="rowbox wdh-100 bottom-box">
			<view v-if="submitflag" class="rowbox wdh-100 send-btn grey-btn" @click="submitInfo">完成</view>
			<view v-else class="rowbox wdh-100 send-btn" @click="submitInfo">完成</view>
		</view>
	</view>
</template>

<script>
	// 环信即时通讯
	import SDK from 'js_sdk/easemob-im-uni-app/Easemob-chat';
	// 实例化 SDK 对象
	const WebIM = wx.WebIM = SDK;
	export default {
		data() {
			return {
				title: '',
				imgList: [],
				submitflag: false,
				describe: '',
				guominshi: '1',
				ganshen: '1',
				puru: '1',
				id: 0
			};
		},
		onLoad(options) {
			this.id = options.id
		},
		computed: {
			user() {
				return this.$store.state.user
			},
		},
		methods: {
			sendMessage(content, img) {
				this.toolflag = false

				let infocontent = {
					classify: 17,
					content: content,
					otherid: typeof(content) == 'number' ? parseInt(content) : 0, //开方ID
					name: this.user.name,
					chatroomheadpic: this.user.logo,
					images: img.length > 0 ? img.join(',') : '',
					guominshi: this.guominshi * 1,
					ganshen: this.ganshen * 1,
					puru: this.puru * 1
				}
				let option = {
					type: "txt",
					msg: JSON.stringify(infocontent),
					to: this.id + "",
					chatType: "singleChat",
				};
				console.log('发送消息=======', option)
				let msg = WebIM.message.create(option);
				getApp().globalData.conn.send(msg).then(res => {
					console.log("=========Send message success==接收消息===", res);
					// uni.hideLoading()
					this.$store.dispatch('actionsHandleNewChatChange', {
						msg: JSON.stringify(infocontent),
						from: this.id + '',
						time: Date.parse(new Date),
						chatdetailflag: true,
						id: res.serverMsgId
					});
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/chat/detail?id=' + this.id
						})
					}, 1000)
				}).catch((e) => {
					console.log("==============Send message fail", e);
				});
			},
			deletImg(index) {
				this.imgList.splice(index, 1)
			},
			addImg() {
				uni.chooseImage({
					success: (res) => {
						res.tempFilePaths.forEach(i => {
							this.imgList.push({
								flag: true,
								url: i
							})
						})
					}
				})
			},



			async submitInfo() {
				if (this.describe == '') {
					uni.showToast({
						title: '请输入病情描述',
						icon: 'none'
					})
				} else {
					if (!this.submitflag) {
						this.submitflag = true
						uni.showLoading({
							title: '正在提交',
							mask: true
						})
						let list = []
						for (var i = 0; i < this.imgList.length; i++) {
							list.push(await this.uploadImgList(this.imgList[i]))
						}
						this.sendMessage(this.describe, list)
						// this.$req.post('/user/fankuiadd', {
						// 	uid: uni.getStorageSync('uid'),
						// 	title: this.title,
						// 	images: list.length > 0 ? list.join(',') : ''
						// }).then(res => {
						// 	if (res.data.code == 0) {
						// 		uni.hideLoading()
						// 		this.submitflag = false
						// 		uni.showModal({
						// 			title: '提交成功',
						// 			complete: () => {
						// 				uni.navigateBack()
						// 			}
						// 		})
						// 	} else {
						// 		uni.hideLoading()
						// 		this.submitflag = false
						// 		uni.showModal({
						// 			title: '提交失败',
						// 			content: res.data.msg
						// 		})
						// 	}
						// })
					} else {

					}
				}

			},
			uploadImgList(img) {
				return new Promise((resolve, reject) => {
					if (img.flag) {
						this.$req.uploadImg(img.url).then(res => {
							resolve(res.data.data)
						})
					} else {
						resolve(img.url)
					}
				})
			},


		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background: #F5F8FB;
		min-height: 100vh;
		justify-content: flex-start;
		font-family: PingFang SC;
	}

	.one-info {
		width: 100%;
		padding: 32rpx 32rpx;
		background: #fff;
		border-bottom: #F5F7F9 solid 1px;
		font-size: 26rpx;

		.info-title {
			font-weight: bold;
			font-size: 26rpx;
			color: #202020;
			margin-right: 20rpx;
		}

		.info-input {
			text-align: left;
			font-size: 26rpx;
		}

		.code-box {
			width: calc(100% - 240rpx);
		}

		.code-input {
			text-align: left;
			font-size: 26rpx;
			width: 200rpx;
		}

		.code-btn {
			font-weight: bold;
		}
	}

	.info-box {
		margin-top: 20rpx;
	}

	.add-img {
		width: 140rpx;
		height: 140rpx;
		border-radius: 20rpx;
	}

	.add-img-box {
		width: 140rpx;
		height: 140rpx;
		position: relative;
		margin-right: 20rpx;
		margin-bottom: 20rpx;

		.delet-img {
			position: absolute;
			top: -10rpx;
			right: -10rpx;
			background: #fff;
			border-radius: 100rpx;
		}
	}

	.tag-list {
		height: 90vh;

		.tag-title {
			width: 100%;
			font-weight: bold;
			font-size: 32rpx;
			color: #202020;
			font-family: PingFang SC;
			margin-top: 32rpx;
		}

		.q-title {
			width: 100%;
			font-weight: bold;
			font-size: 28rpx;
			color: #202020;
			font-family: PingFang SC;
			line-height: 40rpx;
			margin-bottom: 20rpx;
			margin-left: 20rpx;
			margin-top: 40rpx;

			text {
				color: #9b9ea0;
				font-size: 26rpx;
			}
		}

		.q-title>view:nth-child(1) {
			position: relative;
			z-index: 4;
		}

		.q-line {
			background: linear-gradient(to right, #00b6b2, rgba(#00b6b2, 0));
			width: 70rpx;
			height: 15rpx;
			margin-top: -15rpx;
		}

		.tag-list-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: flex-start;
			padding: 0 20rpx;
			box-sizing: border-box;
		}
	}

	.one-tag,
	.sel-tag {
		border: #dadcdc solid 1px;
		color: #474747;
		font-size: 22rpx;
		margin-bottom: 20rpx;
		margin-right: 20rpx;
		border-radius: 4rpx;
		padding: 4rpx 10rpx;
		line-height: 40rpx;
	}

	.sel-tag {
		border: #00b6b2 solid 1px;
		color: #202020;
		background: rgba(#bfdad9, .1);
	}

	.sel-tag-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		padding: 0 20rpx;
		box-sizing: border-box;
		background: #F5F8FB;
		padding-top: 20rpx;
	}

	.scjb-list {
		width: calc(100% - 240rpx);
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
	}

	.one-jigou {
		padding: 24rpx 24rpx;

		.jg-img {
			width: 280rpx;
			height: 200rpx;
			border-radius: 16rpx;
			margin-right: 20rpx;
		}

		.jg-name {
			font-weight: 800;
			font-size: 28rpx;
			color: #000000;
		}

		.jg-area {
			font-weight: 400;
			font-size: 24rpx;
			color: #5F5F5F;
			margin-top: 40rpx;
			width: 100%;
		}
	}

	.search-box {
		padding: 32rpx 36rpx;
		background: #fff;

		.search-info {
			width: 100%;
			height: 70rpx;
			border: #00b6b2 solid 1px;
			border-radius: 100rpx;
			padding-left: 20rpx;

			input {
				font-size: 26rpx;
				margin-left: 10rpx;
			}
		}

		.search-btn {
			width: 110rpx;
			height: 50rpx;
			background: linear-gradient(to right, #01a7a1, #5acbc1);
			border-radius: 100rpx;
			font-size: 26rpx;
			color: #fff;
			margin-right: 10rpx;
		}
	}



	.img-list {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
	}

	.one-img {
		width: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
		margin-top: 20rpx;
	}

	.one-img-box {
		width: 100rpx;
		height: 100rpx;
		margin-right: 40rpx;
		position: relative;
	}

	.close-icon {
		position: absolute;
		right: 0;
		top: 0;
		border-radius: 100rpx;
		background: #fff;
	}

	.grey-btn {
		background: #dee3e7;
	}
</style>
<style>
	.reg-pl {
		color: #939BA7;
	}
</style>